<?php
/**
 * 税收记录
 * @Author    紫梦
 * @DateTime  2018-01-17
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\Taxation as TaxationModel;
use app\common\model\Cars;
use app\common\request\Oss;

class Taxation extends Base
{
	public function index()
	{
        $model = TaxationModel::all();

        $this->assign('data',$model);
        return $this->fetch();
	}

    /**
     * 添加投保记录
     */
    public function add()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            //车辆ID
            $car = Cars::get(['car_unique_id'=>$data['car_unique_id']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车架号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_sn'] = $car['car_sn'];
            $model = new TaxationModel();
            //电子文件
            $file = $request->file('uploadfile');
            if($file)
            {
                $oss = new Oss();
                $data['uploadfile'] = $oss->uploadOss($file);               
            }
               
            $res = $model->allowField(true)->save($data);
            if($res){
                $msg['msg'] = '添加成功';
                
            }else{
                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            

            return $msg;
        }else{
            return $this->fetch();

        }

    }	

    /**
     * 编辑
     */
    public function edit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            //车辆ID
            $car = Cars::get(['car_unique_id'=>$data['car_unique_id']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车架号不存在,请查正后再添加';
                return $msg;

            }

            $data['car_id'] = $car['car_id'];
            $data['car_sn'] = $car['car_sn'];
            $model = new TaxationModel();
             //电子文件
            $file = $request->file('uploadfile');
            if($file)
            {
                $oss = new Oss();
                $data['uploadfile'] = $oss->uploadOss($file);               
            }
            $res = $model->allowField(true)->isUpdate(true)->save($data);
            if($res){

                $msg['msg'] = '更新成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
            }
            

            return $msg;
        }else{
            $model = TaxationModel::get($id);
            $this->assign('data',$model);
            return $this->fetch();

        }
    } 

    /**
     * 删除记录
     */
    public function delete($id)
    {
        try {
            TaxationModel::destroy($id);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }

    /**
     * 删除附件
     */
    public function del_file($id)
    {        
        $url = Db::name('Taxation_file')->where(['id'=>$id])->find();
        $oss = new Oss();
        $res = false;
        if($oss->unlinkOss($url['file_url'])){           
            try {
                Db::name('Taxation_file')->delete($id);
            } catch (Exception $e) {
                return [
                    'code'=>2,
                    'msg'=>$e->getMessage(),
                    ];
            }
            
        }
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }      
}